package com.itheima.reggie.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.FileUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @PostMapping("/submit")
    @Transactional
    public R submitOrder(Orders orders){
        ordersService.submit(orders);
        return R.success("清空购物车成功");
    }

    @GetMapping("/userPage")
    @Transactional
    public R listPage(Integer page,Integer pageSize){
       Page pageBean =  ordersService.listPage(page,pageSize);
       return R.success(pageBean);
    }

    @GetMapping("/page")
    public R pageOrder(Integer page,Integer pageSize,Long number,String beginTime,String endTime){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null,Orders::getId,number)
                .between(beginTime != null && endTime != null,Orders::getOrderTime,beginTime,endTime);
        ordersService.page(ordersPage,queryWrapper);
        return R.success(ordersPage);
    }

    @PutMapping
    public R changeStatus(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    @PostMapping ("/export")
    public void exportExecl(@RequestBody List<Orders> ordersList, HttpServletResponse response) throws IOException {
        System.out.println(ordersList);
//        String fileName ="F:\\gitRepos\\" + System.currentTimeMillis() + ".xlsx";
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        EasyExcel.write(fileName, Orders.class).sheet("模板").doWrite(ordersList);
//        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        //设置ContentType字段告知浏览器返回内容类型
        response.setContentType("application/octet-stream");
        String fileName = URLEncoder.encode("订单明细", "UTF-8").replaceAll("\\+", "%20");
        System.out.println(fileName);
//        response.setHeader("Content-disposition", "attachment;filename=\"订单明细.xlsx\"");
        response.setHeader("Content-Disposition", "attachment;filename="+
                URLEncoder.encode("订单明细.xlsx", "UTF-8"));
        //下面这行代码必须加，否则前端获取不到Content-Disposition字段，即无法获取文件名
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        EasyExcel.write(response.getOutputStream(), Orders.class).autoCloseStream(Boolean.FALSE).sheet("模板").doWrite(ordersList);
//        return R.success("导出成功");
    }
}
